package editor;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import main.Main;
import networking.ClientConnect;
import networking.SMap;


@SuppressWarnings("serial")
public class LevelEditor extends JPanel implements MouseListener, MouseMotionListener
{
	private MapPanel mapPanel;
	private ToolPanel toolPanel;
	private CaptionTextArea textPanel = new CaptionTextArea();
	private int selectedColor;
	private int width;
	private int height;
	private Main main;
	private boolean boxTool = false;
	private int xStart = 0;
	private int yStart = 0;
	private int xInit;
	private int yInit;
	private int xEnd;
	
	private int yEnd;
	private boolean box;
		
	public LevelEditor(int w, int h, Main m)
	{
		toolPanel = new ToolPanel(this, textPanel);
		main = m;
		width = w;
		height = h;
		mapPanel = new MapPanel(width, height);
		selectedColor = Color.blue.getRGB();
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		setSize(601,600);
		this.setLayout(null);
		mapPanel.setSize(501, 500);
		mapPanel.setLocation(100, 0);
		toolPanel.setSize(100, 600);
		toolPanel.setLocation(0, 0);
		toolPanel.setBackground(Color.black);
		textPanel.setSize(501, 100);
		textPanel.setLocation(100, 500);
		add(mapPanel);
		add(toolPanel);
		add(textPanel);
		revalidate();
	}
	
	public LevelEditor(int w, int h, Main m, SMap cMAP) 
	{
		this(w, h, m);
		mapPanel.setMap(cMAP);
	}

	public void paintComponent(Graphics g)
	{
		mapPanel.repaint();
		toolPanel.repaint();
		textPanel.repaint();
	}
	
	public void mouse(MouseEvent e)
	{
		if(boxTool)
		{	
			if(e.getX() - 100 < xInit)
			{
				xStart = e.getX() - 100;
				xEnd = xInit;
				if(xStart < 0)
				{
					xStart = 0;
				}
			}
			else
			{
				xStart = xInit;
				xEnd = e.getX() - 100;
				if(xEnd > width * 10)
				{
					xEnd = width * 10;
				}
			}
			
			if(e.getY() < yStart)
			{
				yStart = e.getY();
				yEnd = yInit;
				if(yStart < 0)
				{
					yStart = 0;
				}
			}
			else
			{
				yStart = yInit;
				yEnd = e.getY();
				if(yEnd > height * 10)
				{
					yEnd = height * 10;
				}
			}
			xStart = xStart - (xStart % 10);
			yStart = yStart - (yStart % 10);
			xEnd = xEnd - (xEnd % 10);
			yEnd = yEnd - (yEnd % 10);
			mapPanel.clearHighLight();
			for(int i = xStart; i < xEnd; i = i + 10)
			{
				for(int j = yStart; j < yEnd; j = j + 10)
				{
					mapPanel.highlight(i, j, selectedColor);
				}
			}
		}
		else
		{
			if(e.getX()>=100 && e.getY()<height*10 && e.getX()< 100+width*10 && e.getY() > 0)
			{
				mapPanel.click(e.getX()-100, e.getY(), selectedColor);
			}
		}
	}

	public void mouseClicked(MouseEvent e) {}

	public void mouseEntered(MouseEvent e) {}

	public void mouseExited(MouseEvent e) {}

	public void mousePressed(MouseEvent e) 
	{
		if(boxTool)
		{
			xInit = e.getX() - 100;
			if(xInit < 0)
			{
				xInit = 0;
			}
			if(xInit > width * 10)
			{
				xInit = width * 10;
			}
			yInit = e.getY();
			if(yInit < 0)
			{
				yInit = 0;
			}
			if(yInit > height * 10)
			{
				yInit = height * 10;
			}
			box = true;
		}
		mouse(e);
	}

	public void mouseReleased(MouseEvent e) 
	{
		if (boxTool && box) 
		{
			for (int i = xStart; i < xEnd; i = i + 10) 
			{
				for (int j = yStart; j < yEnd; j = j + 10) 
				{
					mapPanel.click(i, j, selectedColor);
				}
			}
			box = false;
		}
	}

	public void mouseDragged(MouseEvent e) 
	{
		mouse(e);
	}

	public void mouseMoved(MouseEvent e) {}

	public void changeColor(int rgb) 
	{
		if(rgb == -23456)
		{
			removeMouseListener(this);
			removeMouseMotionListener(this);
			toolPanel.removeListeners();
			main.testMap(mapPanel.testMap());
			setFocusable(false);
		}
		else if(rgb == -20000)
		{
			boxTool = !boxTool;
		}
		else if(rgb == -30000)
		{
			removeMouseListener(this);
			removeMouseMotionListener(this);
			toolPanel.removeListeners();
			main.backToMainMenu();
			setFocusable(false);
		}
		else
		{
			selectedColor = rgb;
		}
	}

	public void saveLevel(String title) 
	{/*//Saving maps to a file
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for(int i=0; i<height; i++)
		{
			for(int j=0; j<width; j++)
			{
				image.setRGB(j, i, mapPanel.getValue(i, j)); 
			}
		}
		
		File outputFile = new File("custom/"+title+".snake");
		try
		{
			ImageOutputStream iOS = ImageIO.createImageOutputStream(outputFile);
			if(!ImageIO.write(image, "png", iOS))
			{
				System.out.println("no appropriate writer found");
			}
			iOS.close();
		} 
		catch (IOException e) 
		{
			System.out.println("Map not written");
			e.printStackTrace();
		}*/
		if(title.equals("Map Name"))
		{
			JOptionPane.showMessageDialog(null, "Please enter map name.");
		}
		else
		{
			if(ClientConnect.Connect())
			{
				ClientConnect.sendMap(new SMap(mapPanel.getMap(), title));
				ClientConnect.Disconnect();
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Error: Could not connect to server");
			}
			
		}
		
	}
}
